<?php

namespace app\admin\model\course;

use think\Model;


class OrderGoods extends Model
{
    // 表名
    protected $name = 'course_order_goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;



    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function teacher()
    {
        return $this->belongsTo('app\admin\model\User', 'teacher_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function course()
    {
        return $this->belongsTo('app\admin\model\course\Course', 'course_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function courseOrder()
    {
        return $this->belongsTo('app\admin\model\course\Order', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    /*
     * 获取学员列表
     * */
    public function getStudentList($condition,$order_goods_id){
        $orderGoods = $this->get($order_goods_id);

        if(!$orderGoods){
//            return [];
            exception("未找到当前课表");
        }
        $studentList = $this
            ->where("teacher_id",$orderGoods['teacher_id'])
            ->where("course_id",$orderGoods['course_id'])
            ->where("school_id",$orderGoods['school_id'])
            ->where("classes_id",$orderGoods['classes_id'])
            ->where("w",$orderGoods['w'])
            ->where("time_no",$orderGoods['time_no'])
            ->where("status",">=",Order::STATUS_PAY)
            ->where($condition)
            ->field("id,student_name,student_id,is_xiaojie,order_id")
            ->select();
        return $studentList;
    }


    /*
     * 获取课程表
     * */
    public function getTimeTable($condition,$day = "", $is_list = 1, $status_arr = [Order::STATUS_PAY]){

        if(!empty($day)){
//            $day = date("Y-m-d");
            $this_w = date("w",strtotime($day));
            $wArr = ClassesConfig::getWArr($this_w);
            $condition['order_goods.w'] =  ["in", $wArr];
//            dump(strtotime($day));exit;
            $condition['courseOrder.pay_time'] =  ["<", strtotime($day)+86400];
        }

        $list = $this->with(["course"=>function($query){
            $query->withField("name,image,desc");
        },"courseOrder"=>function($query){
            $query->withField("pay_time,class_hour");
        },"teacher"=>function($query){
            $query->withField("nickname");
        }]);

        if(count($status_arr) > 0){
            $list = $list->where([
                "order_goods.status" => ["in", $status_arr],
            ]);
        }


        $list = $list->where($condition)
            ->group("school_id,classes_id,w,course_id,time_no");
        if($is_list){
            $list = $list->select();
        }else{
            $find = $list->find();
            if(!$find){
                exception("未找到当前课程");
            }
            return  $find;
        }
        foreach ($list as $key => $value){
            if(isset($value['course']['image'])){
                $value['course']['image'] = deal_img_url($value['course']['image']);
            }
        }
        return $list;
    }



}
